var gridObj;

$(function() {
    gridObj = $.fn.bsgrid.init('staffTable', {
        dataType: 'json',
        url: '/admin/queryUser',
        pageSize: 10,
        pageSizeSelect: true,
        pageSizeForGrid: [5, 10, 15, 20],
        displayBlankRows: false,
        stripeRows: true,
        rowHoverColor: true
    });


    $(window).load(function() {
        //查找所有的小组
        $.ajax({
            url: '/admin/queryTeam',
            type: 'POST',
            success: function(data) {
                $.each(data, function(index, value) {
                    $('#tid,#dteam').append('<option value="' + value.id + '">' + value.name + '</option>');
                });
            }
        });
    });
});

//操作
function operate(record, rowIndex, colIndex, options) {
    return '<button type="button" class="btn btn-primary btn-xs" onclick="management.modifyBtn(' + rowIndex + ');">修改</button>  ' +
        '  <button type="button" class="btn btn-primary btn-xs" onclick="management.deleteUser(' + gridObj.getRecordIndexValue(record, 'id') + ');">删除</button> ' +
        '  <button type="button" class="btn btn-primary btn-xs" onclick="management.startOrStop(' + rowIndex + ');">启停</button>';
}
//状态标红
function mark(record, rowIndex, colIndex, options) {
    var status = gridObj.getRecordIndexValue(record, 'status');
    if (status == 0) {
        return '<span class="red">' + '停用' + '</span>';
    } else {
        return '<span >' + '启用' + '</span>';
    }
}

var management = {
    init: function() {
        var self = this;

        self.addEvent();
        self.validate();
    },
    // 绑定事件
    addEvent: function() {
        var self = this;

        $('#search').click(function() {
            self.search();
        });

        $('#add').click(function() {
            self.getMaxNumber();
            self.showDialog();
        });

        $('#confirmAdd').click(function() {
            if ($("#signupForm").valid()) {
                self.confirmAdd();
            }
        });

        $('#closeDialog').click(function() {
            self.closeDialog();
        });

        $('#close').click(function() {
            self.closeDialog();
        });
    },
    showDialog: function() {
        $('#dialog').modal('show');
    },
    //关闭并清空表单
    closeDialog: function() {
        $('#dialog').modal('hide');
        $('#dialog form')[0].reset();
        $('#uid').val(0);
        $('#upid').val(0);
        $('#signupForm .error').text('');
    },

    //搜索
    search: function() {
        var params = {
            number: $('#number').val(),
            name: $('#name').val(),
            tid: $('#tid').val(),
            sex: $('#sex').val()
        };

        gridObj.search(params);

    },
    //删除
    deleteUser: function(uid) {
        if (confirm('确认删除？')) {
            $.ajax({
                type: 'POST',
                url: '/admin/deleteUser',
                data: {
                    uid: uid
                },
                success: function() {
                    block.showMsg('删除成功');
                    //alert('删除成功');
                    //刷新
                    gridObj.refreshPage();
                },
                error: function(data) {
                    alert('出错了');
                },
            });
        }
    },
    //修改按钮
    modifyBtn: function(rowIndex) {
        management.showDialog();
        var user = gridObj.getRecord(rowIndex);
        //alert(JSON.stringify(user));
        $('#dnumber').val(user.number);
        $('#dname').val(user.name);
        $('#dsex').val(user.sex);
        $('#dteam').val(user.tid);
        $('#dposition').val(user.position);
        $('#uid').val(user.id);
        $('#upid').val(user.upid);
    },

    //提交
    confirmAdd: function() {
        var data = {
            number: $('#dnumber').val(),
            name: $('#dname').val(),
            sex: $('#dsex').val(),
            tid: $('#dteam').val(),
            position: $('#dposition').val(),
            perm: $('#dpermission').val(),
            pid: $('#dpermission').val()
        };
        var uid = $('#uid').val();
        if (uid) {
            data = $.extend(data, {
                id: uid
            });
            data = $.extend(data, {
                upid: $('#upid').val()
            });
        }
        //alert(JSON.stringify(data));
        management.save(data);
        management.closeDialog();
    },

    //启停
    startOrStop: function(rowIndex) {
        var user = gridObj.getRecord(rowIndex);
        //alert(JSON.stringify(user));
        var data = {
            id: user.id,
            status: user.status == 0 ? 1 : 0,
        };
        management.save(data);
    },
    //更新user
    save: function(data) {
        $.ajax({
            type: 'POST',
            url: '/admin/saveUser',
            data: data,
            success: function() {
                //刷新
                gridObj.refreshPage();
            },
            error: function(data) {
                alert('出错了');
            },
        });
    },

    //获取最大的工号
    getMaxNumber: function() {
        $.ajax({
            type: 'POST',
            url: '/admin/getMaxNumber',
            success: function(data) {
                //alert(JSON.stringify(data[0].maxNum));
                $('#dnumber').val(data[0].maxNum + 1);
            }
        });
    },

    validate: function() {
        $("#signupForm").validate({
            rules: {
                dnumber: "required",
                dname: "required",
                dteam: "required",
            },
            messages: {
                dnumber: "请输入工号",
                dname: "请输入姓名",
                dteam: "请选择小组",

            }
        });
    }
};
management.init();
